package com.yoki.loginview;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class CloudActivity extends AppCompatActivity {

    private String login_JSON = "";
    private int SERVER_GET_TRUE = 0;
    private int SERVER_GET_FALSE = 1;
    private EditText UserTB;
    private EditText PwdTB;
    private String server_url = "http://api.nlecloud.com/Users/Login";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cloud);
        UserTB = findViewById(R.id.CUserTB);
        PwdTB = findViewById(R.id.CPwdTB);
    }

    private Myhandler handler = new Myhandler();
    private class Myhandler extends Handler
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==SERVER_GET_TRUE)
            {
                StringBuilder builder = (StringBuilder) msg.obj;
                Toast.makeText(CloudActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
            }
            else if(msg.what==SERVER_GET_FALSE)
            {
                Toast.makeText(CloudActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
            }
        }
    }

    private class networkRunnable implements Runnable{
        @Override
        public void run() {
            HttpURLConnection connect = null;
            BufferedReader reader = null;
            try
            {
                URL api = new URL(server_url);
                connect = (HttpURLConnection)api.openConnection();
                connect.setRequestMethod("POST");
                connect.setConnectTimeout(5000);
                connect.setReadTimeout(5000);
                connect.setRequestProperty("connection","Keep-Alive");
                connect.setRequestProperty("Charset","UTF-8");
                connect.setRequestProperty("Content-Type","application/json");

                OutputStream outputStream = connect.getOutputStream();
                outputStream.write(login_JSON.getBytes());
                outputStream.flush();
                outputStream.close();
                Message msg = Message.obtain();
                if(connect.getResponseCode()==200)
                {
                    InputStream inputStream = connect.getInputStream();
                    reader = new BufferedReader(new InputStreamReader(inputStream));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while((line=reader.readLine())!=null)
                    {
                        response.append(line);
                    }
                    Log.e("TAG","云服务器返回："+response.toString());
                    msg.what = SERVER_GET_TRUE;
                    msg.obj = response;
                    handler.sendMessage(msg);
                }
                else
                {
                    Log.e("TAG","登录失败，返回结果为："+connect.getResponseCode());
                    msg.what = SERVER_GET_FALSE;
                    msg.obj = connect.getResponseCode();
                    handler.sendMessage(msg);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    public void CloudLogin(View view)
    {
        String UserText = UserTB.getText().toString();
        String PwdText = PwdTB.getText().toString();
        login_JSON = "{\n" +
                "  \"Account\": \""+UserText+"\",\n" +
            "  \"Password\": \""+PwdText+"\",\n" +
            "  \"IsRememberMe\": true\n" +
            "}";
        Thread thread = new Thread(new networkRunnable());
        thread.start();
    }
}
